import 'package:easy_refresh/easy_refresh.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/model/exchange_model.dart';

const _pageSize = 10;

class VipExchangePageController extends BaseRefreshController
    with BaseRefreshPageCounter {
  RxInt selectIndex = 0.obs;
  List<ExchangeModel> items = List<ExchangeModel>.of([]);
  Future exchange() async {
    final resp = await Api.exchange(satisfyType: selectIndex.value + 1);
    if (resp) {
      EasyToast.show('提交成功');
    }
  }

  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();
    List<ExchangeModel>? resp =
        await Api.getExchangeRecord(page: page, pageSize: _pageSize);

    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    List<ExchangeModel>? resp =
        await Api.getExchangeRecord(page: page, pageSize: _pageSize);
    if (resp == null) {
      return IndicatorResult.fail;
    }
    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }
    incPage();

    return IndicatorResult.success;
  }

  void _setList(List<ExchangeModel>? list) {
    items.clear();
    _addList(list);
  }

  void _addList(List<ExchangeModel>? list) {
    if (null != list && list.isNotEmpty) {
      items.addAll(list);
      update();
    }
  }
}
